package model_convert

import (
	"strings"
)

// help generate go server side pprof

func GenerateNginxConf(serverName string, host string, port string) string {
	var tmpl = `
server {
    listen       80;
    server_name  ${server_name};

    # request header

    proxy_set_header   Host             $http_host;
    proxy_set_header   Cookie           $http_cookie;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-Proto    $scheme;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    location  / {
    }
    location /ping {
       return 200 'pone';
    }
    location /debug/pprof/ {
       proxy_pass http://${host}${port};
    }
}
`

	rs := strings.Replace(tmpl, "${server_name}", serverName, -1)
	rs = strings.Replace(rs, "${host}", host, -1)
	rs = strings.Replace(rs, "${port}", port, -1)

	return rs
}

func GenerateGoProfSdk(port string) string {
	var tmpl = `
    // Auto generated by github.com/fwhezfwhez/model_convert.GenerateGoProfSdk()
    // Don't forget import (_ "net/http/pprof")
    go func() {
       var url = fmt.Sprintf("localhost%s", "${port}")

       if e := http.ListenAndServe(url, nil); e != nil {
		    fmt.Println("prof_at:error", errorx.Wrap(e).Error())
		    return
	    }
    }()
`
	return strings.Replace(tmpl, "${port}", port, -1)
}

func GenerateProf(host string, port string, serverName string) (string, string) {
	return GenerateNginxConf(serverName, host, port), GenerateGoProfSdk(port)
}
